import util from 'service/util'
import calendar from 'service/util/calendar.js'

var data = (function () {
    var now = new Date()
    var year = now.getFullYear()
    var yue = now.getMonth() + 1
    var date = now.getDate()
    return {
        months: createMonths(year, yue),
        pos: {
            l: 0,
            startL: 0,
            index: 0,
            year,
            yue,
            transitionTime: 0
        },
        touch: {
            startX: 0,
            startTime: 0,
            disX: 0,
            moving: false
        },
        today: {
            year,
            month: yue - 1,
            date
        },
        time: {
            repayDayTime: '',
            todayTime: new Date(year, yue - 1, date).getTime()
        }

    }
})()

function createMonths(year, yue) {
    var month = createMonth(year, yue)
    var lastMonth = createLastMonth(year, yue)
    var nextMonth = createNextMonth(year, yue)
    return {
        '-1': lastMonth,
        0: month,
        1: nextMonth
    }
}

// yue 从 1 - 12
// month 从 0 - 11
// date 几号
function createMonth(year, yue) {
    // 一周
    var weeks = []
    var month = yue - 1
    weeks.push(createWeek(year, month, 1))
    nextWeek(year, month, 1, weeks)
    return {
        year, yue, weeks
    }
}

function createLastMonth(year, yue) {
    var lastYue = yue == 1 ? 12 : yue - 1
    var lastYear = lastYue == 12 ? year - 1 : year
    return createMonth(lastYear, lastYue)
}

function createNextMonth(year, yue) {
    yue = yue == 12 ? 1 : yue + 1
    year = yue == 1 ? year + 1 : year
    return createMonth(year, yue)
}

function nextWeek(year, month, date, weeks) {
    var lastWeekFirstDay = weeks[weeks.length - 1][0]
    var nextWeekTime = new Date(lastWeekFirstDay.year, lastWeekFirstDay.month, lastWeekFirstDay.date).getTime() + 7 * 24 * 60 * 60 * 1000
    var nextWeekDate = new Date(nextWeekTime)
    if (nextWeekDate.getMonth() == month) {
        year = nextWeekDate.getFullYear()
        month = nextWeekDate.getMonth()
        date = nextWeekDate.getDate()
        weeks.push(createWeek(year, month, date))
        nextWeek(year, month, date, weeks)
    }
}

function createWeek(year, month, date) {
    var week = []
    week.year = year
    week.month = month
    week.push(createDay(year, month, date))
    lastDay(year, month, date, week)
    nextDay(year, month, date, week)
    return week
}

function lastDay(year, month, date, week) {
    var lastDate = new Date((new Date(year, month, date).getTime() - 1))
    if (lastDate.getDay() != 6) {
        year = lastDate.getFullYear()
        month = lastDate.getMonth()
        date = lastDate.getDate()
        week.splice(0, 0, createDay(year, month, date))
        lastDay(year, month, date, week)
    }
}

function nextDay(year, month, date, week) {
    var nextDate = new Date((new Date(year, month, date).getTime() + 24 * 60 * 60 * 1000))
    if (nextDate.getDay() != 0) {
        year = nextDate.getFullYear()
        month = nextDate.getMonth()
        date = nextDate.getDate()
        week.push(createDay(year, month, date))
        nextDay(year, month, date, week)
    }
}

function createDay(year, month, date) {
    return {
        year: year,
        month: month,
        date: date
    }
}

var vm = {
    data() {
        return data
    },
    props: ['loanDay', 'repayDay'],
    mounted() {
        var repayDay = this.repayDay
        this.time.repayDayTime = new Date(repayDay.year, repayDay.month, repayDay.date).getTime()
    },
    methods: {
        start(e) {
            e.preventDefault()
            if (this.touch.moving) {
                return
            }
            var touch = e.touches[0]
            this.touch.startX = touch.clientX
            this.touch.startTime = new Date().getTime()
            this.pos.startL = this.pos.l
            this.touch.disX = 0
            var move = (e) => {
                this.touch.moving = true
                e.preventDefault()
                touch = e.touches[0]
                this.touch.disX = touch.clientX - this.touch.startX
                this.pos.l = this.pos.startL + this.touch.disX
            }
            var end = (e) => {
                e.preventDefault()
                var winWidth = util.winWidth()
                if (Math.abs(this.touch.disX) < 10) {
                    this.pos.index = 0
                } else if (new Date().getTime() - this.touch.startTime < 300) {
                    // 滑动时间小于300ms，属于快速滑动，切换日历
                    if (this.touch.disX > 0) {
                        this.pos.index--
                        this.pos.yue--
                        if (this.pos.yue == 0) {
                            this.pos.yue = 12
                            this.pos.year--
                        }
                    } else {
                        this.pos.index++
                        this.pos.yue++
                        if (this.pos.yue == 13) {
                            this.pos.yue = 1
                            this.pos.year++
                        }
                    }
                } else {
                    this.pos.index -= Math.round(this.touch.disX / winWidth)
                    if (this.pos.index > 0) {
                        this.pos.yue++
                        if (this.pos.yue == 13) {
                            this.pos.yue = 1
                            this.pos.year++
                        }
                    } else if (this.pos.index < 0) {
                        this.pos.yue--
                        if (this.pos.yue == 0) {
                            this.pos.yue = 12
                            this.pos.year--
                        }
                    }
                }
                this.pos.transitionTime = 0.3
                this.pos.l = -this.pos.index * winWidth
                window.removeEventListener('touchmove', move)
                window.removeEventListener('touchend', end)
            }
            window.addEventListener('touchmove', move)
            window.addEventListener('touchend', end)
        },
        trEnd(e) {
            e.preventDefault()
            this.pos.transitionTime = 0
            this.pos.index = 0
            this.pos.l = 0
            this.months = createMonths(this.pos.year, this.pos.yue)
            this.$nextTick(() => {
                this.touch.moving = false
            })
        },
        setMonthTitle(yue) {
            var monthWords = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
            return monthWords[yue - 1] + '月'
        },
        // 是否是借款日
        isLoanDay(day) {
            return this.judgeTwoDays(day, this.loanDay) == 0
        },
        // 是否是还款日
        isRepayDay(day) {
            // 判断是否是还款日
            return this.judgeTwoDays(day, this.repayDay) == 0
        },
        // 是否是逾期日期
        isOverdue(day) {
            return this.judgeTwoDays(day, this.repayDay) > 0 && this.judgeTwoDays(day, this.today) < 0
        },
        // 是否是今天
        isToday(day) {
            return this.judgeTwoDays(day, this.today) == 0
        },
        // 在还款期内
        inLoan(day) {
            if (this.judgeTwoDays(day, this.today) == 0 && (this.isLoanDay(day) || this.isRepayDay(day))) {
                return false
            }
            return this.judgeTwoDays(day, this.loanDay) >= 0 && this.judgeTwoDays(day, this.repayDay) <= 0
        },
        // 判断两天的关系
        judgeTwoDays(dayOne, dayTwo) {
            // 同一天：0
            // one在前：<0
            // one在后：>0
            return new Date(dayOne.year, dayOne.month, dayOne.date).getTime() - new Date(dayTwo.year, dayTwo.month, dayTwo.date).getTime()
        },
        // 设置下面字的显示
        setSolar2lunar(day) {
            // 判断是否是借款日
            if (this.isLoanDay(day)) {
                return '●借款'
            }
            // 判断是否是还款日
            if (this.isRepayDay(day)) {
                return '●还款'
            }
            // 判断逾期
            if (this.isOverdue(day)) {
                return '逾期'
            }
            return calendar.solar2lunar(day.year, day.month, day.date).IDayCn
        },
        // 判断日子的状态
        inMonth(week, day) {
            var classString = ''
            // 在月内
            if (week.month == day.month) {
                classString += 'inMonth'
            }
            // 借款
            if (this.isLoanDay(day)) {
                classString += ' loanDay'
            }
            // 还款
            if (this.isRepayDay(day)) {
                classString += ' repayDay'
            }
            // 逾期
            if (this.isOverdue(day)) {
                classString += ' overdue'
            }
            // 今天
            if (this.isToday(day)) {
                classString += ' today'
            }
            // 在借款期限
            if (this.inLoan(day)) {
                classString += ' inLoan'
            }
            return classString
        }
    }
}

export default vm